//
//  ZJDesThirdPhotoDetailViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/25.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJDesThirdPhotoDetailViewController.h"
#import "ZJDesThirdMorePhotoModel.h"
@interface ZJDesThirdPhotoDetailViewController ()<UIScrollViewDelegate>

@property(nonatomic,retain)UIScrollView *bgScrollView;
@property(nonatomic,retain)UILabel *trip_nameLabel;
@property(nonatomic,retain)UILabel *textLabel;
@property(nonatomic,retain)UIView *headView;
@property(nonatomic,retain)UIView *footView;
@property(nonatomic,assign)BOOL isSelected;

@end

@implementation ZJDesThirdPhotoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.isSelected = NO;
    [self createView];
}

- (void)createView{
    
    
    
    self.bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.bgScrollView.contentSize = CGSizeMake(WIDTH*self.photoArray.count, self.bgScrollView.H );
    self.bgScrollView.delegate = self;
    self.bgScrollView.pagingEnabled = YES;
    [self.view addSubview:self.bgScrollView];

    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    [self.view addSubview:self.headView];

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(15, 20+10, 20, 20);
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"add_new_poi_back_btn"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    self.trip_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake((WIDTH-250)/2, button.Y, 250, 20)];
    self.trip_nameLabel.textColor = [UIColor whiteColor];
    self.trip_nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.headView addSubview:self.trip_nameLabel];
    
    self.footView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-100, WIDTH, 100)];
    [self.view addSubview:self.footView];
    
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, self.footView.W -10, self.footView.H -40)];
    self.textLabel.numberOfLines = 0;
    self.textLabel.font = [UIFont systemFontOfSize:15];
    self.textLabel.textColor = [UIColor whiteColor];
    [self.footView addSubview:self.textLabel];
    
    self.trip_nameLabel.text = [[self.photoArray objectAtIndex:self.i] trip_name];
    
//    CGFloat textHeight = [ZJAppTool heightForContent:[[self.photoArray objectAtIndex:self.i] text] width:self.textLabel.W  size:15];
    
    CGSize size = [SizeforLabel labelRectWithSize:CGSizeMake(self.textLabel.W, 1000) LabelText:[[self.photoArray objectAtIndex:self.i] text] Font:[UIFont systemFontOfSize:15]];
    
    self.footView.frame = CGRectMake(0, HEIGHT-size.height-10, WIDTH, size.height);
    self.textLabel.frame = CGRectMake(5, 5, self.textLabel.W , size.height);
    self.textLabel.text = [[self.photoArray objectAtIndex:self.i] text];
    
    
    for (int i = 0; i < self.photoArray.count; i++) {
        
        CGFloat pWidth = [[[self.photoArray objectAtIndex:i] infoModel] w];
        CGFloat pHeight = [[[self.photoArray objectAtIndex:i] infoModel] h];
        CGFloat height = pHeight*WIDTH/pWidth;
        UIImageView *photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake((0+i)*WIDTH, (self.bgScrollView.H-height)/2, WIDTH, height)];
        photoImageView.userInteractionEnabled = YES;
        NSURL *url = [NSURL URLWithString:[[self.photoArray objectAtIndex:i] photo]];
        [photoImageView sd_setImageWithURL:url placeholderImage:IMAGE];
        [self.bgScrollView addSubview:photoImageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [photoImageView addGestureRecognizer:tap];
        
    }
    
    [self.bgScrollView setContentOffset:CGPointMake(self.i*WIDTH, 0)];
    
}
//翻页执行的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int i = scrollView.contentOffset.x/self.bgScrollView.W ;
    self.trip_nameLabel.text = [[self.photoArray objectAtIndex:i] trip_name];
//    CGFloat textHeight = [ZJAppTool heightForContent:[[self.photoArray objectAtIndex:i] text] width:self.textLabel.W  size:15];
    CGSize size = [SizeforLabel labelRectWithSize:CGSizeMake(self.textLabel.W, 10000) LabelText:[[self.photoArray objectAtIndex:i] text] Font:[UIFont systemFontOfSize:15]];
    
    if (self.isSelected == NO) {
        self.footView.frame = CGRectMake(0, HEIGHT-size.height-10, WIDTH, size.height+10);
        self.textLabel.frame = CGRectMake(5, 5, self.textLabel.W , size.height);
        self.textLabel.text = [[self.photoArray objectAtIndex:i] text];
    }else{
        self.footView.frame = CGRectMake(0, HEIGHT+size.height+10, WIDTH, size.height+10);
        self.textLabel.frame = CGRectMake(5, 5,HEIGHT +self.textLabel.W , size.height);
        self.textLabel.text = [[self.photoArray objectAtIndex:i] text];
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.isSelected == NO) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, -70, WIDTH, 70);
            self.footView.frame = CGRectMake(0, HEIGHT+100, WIDTH, 100);
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, 0, WIDTH, 70);
            self.footView.frame = CGRectMake(0, HEIGHT-self.footView.H , WIDTH, self.footView.H);
        }];
    }
    self.isSelected = !self.isSelected;
}




//返回按钮
- (void)buttonAction:(UIButton *)but{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
